#include "client.h"

//注册账户
int register_user(int sockfd, char *username, char *password){
    //非传输文件
    train_t *train = (train_t *)calloc(1, sizeof(train_t));

    //发送用户名和密码
    train->type = USER_REGISTER;
    strcpy(train->data, username);
    strcat(train->data, " ");
    strcat(train->data, password);
    train->length = strlen(train->data);
    send(sockfd, &train->type, sizeof(train->type), 0);
    send(sockfd, &train->length, sizeof(train->length), 0);
    send(sockfd, train->data, train->length, 0);


    //接收响应
    bzero(train, sizeof(train_t));
    recv1(sockfd, &train->type, sizeof(train->type));
    recv1(sockfd, &train->length, sizeof(train->length));
    recv1(sockfd, train->data, train->length);

    //用户名是否存在,存在注册失败，不用注册
    if(train->type == REGISTER_FAILD){
        if(strcmp(train->data, "用户已存在") == 0){
            printf("%s", train->data);   //已注册
            printf("请直接登录\n");
            fflush(stdout);
            free(train);
            return -1;
        }else{//格式错误
            printf("%s\n", train->data);
            free(train);
            return -2;
        }
    }else{   //注册成功
        printf("%s\n", train->data);
    }
    free(train);
    return 0;
}



//登录账户
int log_in(int sockfd, char *username, char *password){

    //非传输文件
    train_t *train = (train_t *)calloc(1, sizeof(train_t));


    //发送用户名和密码
    train->type = USER_LOGIN;
    strcpy(train->data, username); 
    strcat(train->data, " ");
    strcat(train->data, password);
    train->length = strlen(train->data);

    send(sockfd, &train->type, sizeof(train->type), 0);
    send(sockfd, &train->length, sizeof(train->length), 0);
    send(sockfd, train->data, train->length, 0);


    //接收响应
    bzero(train, sizeof(train_t));
    recv1(sockfd, &train->type, sizeof(train->type));
    recv1(sockfd, &train->length, sizeof(train->length));
    recv1(sockfd, train->data, train->length);

    //处理响应,登录成功
    if(train->type == LOGIN_SUCESS){
        printf("%s\n", train->data);            //登录成功，执行后续操作
        free(train);
        return 0;                               //登录成功
    }

    //登录失败
    if(train->type == LOGIN_FAILD){
        //用户未注册
        if(strcmp(train->data, "没有该用户!") == 0){
            printf("%s\n", train->data);
            fflush(stdout);
            free(train);
            return -1;
        }
        //密码错误
        else if(strcmp(train->data, "密码错误!") == 0){
            printf("%s\n", train->data);
            free(train);
            return -2;
        }
        //格式错误
        else{
            printf("%s\n", train->data);
            free(train);
            return -3;
        }
    }
    return 0;
}


//隐藏密码
void get_password(char *buf, int max_len) {
    struct termios oldt, newt;
    tcgetattr(STDIN_FILENO, &oldt);    // 获取当前终端设置
    newt = oldt;
    newt.c_lflag &= ~(ECHO | ICANON);  // 禁用回显和行缓冲
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);  // 应用新设置

    int i = 0;
    char ch;
    while (read(STDIN_FILENO, &ch, 1) == 1 && ch != '\n' && i < max_len-1) {
        if (ch == 127 && i > 0) {      // 处理退格键 (ASCII 127)
            i--;
            write(STDOUT_FILENO, "\b \b", 3);  // 删除星号
        } else if (ch >= 32 && ch <= 126) {  // 可打印字符
            buf[i++] = ch;
            write(STDOUT_FILENO, "*", 1);    // 显示星号
        }
    }
    buf[i] = '\0';  // 字符串终结符
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);  // 恢复原始设置
    printf("\n");
}

